﻿//---------------------------------------------------------------------------
// <copyright file="RibbonApplicationSplitMenuItem.cs" company="Microsoft Corporation">
//     Copyright (C) Microsoft Corporation.  All rights reserved.
// </copyright>
//---------------------------------------------------------------------------

namespace Microsoft.Windows.Controls.Ribbon
{
    using System;
    using System.Windows;
    using System.Windows.Controls;
    using System.Windows.Input;

    /// <summary>
    ///   Implements a SplitMenuItem in the Ribbon's ApplicationMenu.
    /// </summary>
    public class RibbonApplicationSplitMenuItem : RibbonApplicationMenuItem
    {
        /// <summary>
        ///   Initializes static members of the RibbonApplicationSplitMenuItem class.
        /// </summary>
        static RibbonApplicationSplitMenuItem()
        {
            Type ownerType = typeof(RibbonApplicationSplitMenuItem);

            DefaultStyleKeyProperty.OverrideMetadata(ownerType, new FrameworkPropertyMetadata(ownerType));
            EventManager.RegisterClassHandler(ownerType, MouseLeftButtonDownEvent, new MouseButtonEventHandler(OnMouseLeftButtonUp));
        }
     
        /// <summary>
        ///   Called when the SplitMenuItem recieves MouseLeftButtonUp, in which case we
        ///   want to close the SplitMenuItem's popup.
        /// </summary>
        /// <param name="sender">The SplitMenuItem recieving the event.</param>
        /// <param name="e">The event data.</param>
        private static void OnMouseLeftButtonUp(object sender, MouseButtonEventArgs e)
        {
            RibbonApplicationSplitMenuItem rasmi = (RibbonApplicationSplitMenuItem)sender;

            FrameworkElement originalSource = e.OriginalSource as FrameworkElement;

            if (originalSource == null || Grid.GetColumn(originalSource) != 2)
            {
                Mouse.Capture(null); // Close the menu
                rasmi.OnClick();
            }
        }
    }
}